

package com.hazelcast.client.impl.protocol.task.crdt.pncounter;

import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.PNCounterGetConfiguredReplicaCountCodec;
import com.hazelcast.client.impl.protocol.task.AbstractCallableMessageTask;
import com.hazelcast.crdt.pncounter.PNCounter;
import com.hazelcast.instance.impl.Node;
import com.hazelcast.internal.nio.Connection;
import com.hazelcast.security.SecurityInterceptorConstants;
import com.hazelcast.security.permission.ActionConstants;
import com.hazelcast.security.permission.PNCounterPermission;

import java.security.Permission;

/**
 * Task responsible for processing client messages for returning the max
 * configured replica count for a {@link PNCounter}.
 */
public class PNCounterGetConfiguredReplicaCountMessageTask extends AbstractCallableMessageTask<String> {


    public PNCounterGetConfiguredReplicaCountMessageTask(ClientMessage clientMessage, Node node, Connection connection) {
        super(clientMessage, node, connection);
    }

    @Override
    protected String decodeClientMessage(ClientMessage clientMessage) {
        return PNCounterGetConfiguredReplicaCountCodec.decodeRequest(clientMessage);
    }

    @Override
    protected ClientMessage encodeResponse(Object response) {
        return PNCounterGetConfiguredReplicaCountCodec.encodeResponse((Integer) response);
    }

    @Override
    protected Object call() throws Exception {
        return nodeEngine.getConfig().findPNCounterConfig(parameters).getReplicaCount();
    }

    @Override
    public String getServiceName() {
        return SecurityInterceptorConstants.PN_COUNTER_SERVICE;
    }

    @Override
    public Object[] getParameters() {
        return null;
    }

    @Override
    public Permission getRequiredPermission() {
        return new PNCounterPermission(parameters, ActionConstants.ACTION_READ);
    }

    @Override
    public String getMethodName() {
        return SecurityInterceptorConstants.GET_CONFIGURED_REPLICA_COUNT;
    }

    @Override
    public String getDistributedObjectName() {
        return parameters;
    }
}
